package com.anysoft.me.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import com.anysoft.bean.UserBean;
import com.anysoft.utils.MentorLogger;
import com.anysoft.utils.PreferencesUtils;
import com.anysoft.utils.user.UserBeanUtil;

import java.util.List;

/**
 * Created by lty on 2018/3/26.
 */

public class AppInstallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        PackageManager manager = context.getPackageManager();
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            MentorLogger.Loge("app", "安装成功===="+packageName);
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            installDoSomething(context);
            MentorLogger.Loge("app", "卸载成功===="+packageName);
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
//            installDoSomething(context);
            MentorLogger.Loge("app", "替换成功===="+packageName);
        }
    }

    private void installDoSomething(Context context) {
        List<UserBean> userList = UserBeanUtil.getInstance().findUserList();
        if (userList.size() > 0){
            for (int i = 0; i < userList.size(); i++) {
                if (userList.get(i).getIsFingerPrintOpen()) {
                    UserBeanUtil.clearFingerPrint(userList.get(i).getUserId());
                }
                if (userList.get(i).getIsGestureOpen()){
                    UserBeanUtil.clearGesture(userList.get(i).getUserId());
                }
            }
        }
        PreferencesUtils.clearPreference(context);
    }
}
